package com.briup.app08.service.impl;

import com.briup.app08.bean.Account;
import com.briup.app08.dao.AccountMapper;
import com.briup.app08.service.IAccountService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

@Service
public class AccountServiceimpl implements IAccountService {
    @Resource
    private AccountMapper accountMapper;

    @Override
    public void save(Account account) {
        //账号不能同名
        //账号默认状态为正常
        Account dbAccount=accountMapper.selectByAccount(account.getAccount());
        if (dbAccount!=null){
            throw new RuntimeException("账号已被占用！");
        }
        account.setStatus("正常");
        account.setLoginTime(new Date().getTime());
        accountMapper.insert(account);
    }

    @Override
    public List<Account> findAll() {return accountMapper.selectAll();
    }

    @Override
    public  Account findById(long id) {return accountMapper.selectByPrimaryKey(id);}

    @Override
    public void deleteById(long id) {
        Account account=this.findById(id);
        if (account==null){
            throw new RuntimeException("该账号不存在");
        }
        accountMapper.deleteByPrimaryKey(id);
    }

}
